home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / zendisk2.zip / LST11-18.ASM < prev    next >
Assembly Source File  |  1990-02-15  |  3KB  |  113 lines

  1. ;
  2. ; *** Listing 11-18 ***
  3. ;
  4. ; Demonstrates the calculation of the element number in a
  5. ; look-up table of a byte matching the ASCII value of a
  6. ; keystroke when SCASB is used, where the 1-count
  7. ; overrun of SCASB must be compensated for. The element
  8. ; number in the look-up table is used to look up the
  9. ; corresponding address in a second table; that address is
  10. ; then jumped to in order to handle the keystroke.
  11. ;
  12. ; This is a standalone program, not to be used with PZTIME
  13. ; but rather assembled, linked, and run by itself.
  14. ;
  15. stack    segment    para stack 'STACK'
  16.     db    512 dup (?)
  17. stack    ends
  18. ;
  19. code    segment    para public 'CODE'
  20.     assume    cs:code, ds:nothing
  21. ;
  22. ; Main loop, which simply calls VectorOnASCIIKey until one
  23. ; of the key handlers ends the program.
  24. ;
  25. start    proc    near
  26.     call    VectorOnASCIIKey
  27.     jmp    start
  28. start    endp
  29. ;
  30. ; Gets the next 16-bit key code from the BIOS, looks up just
  31. ; the 8-bit ASCII portion in ASCIIKeyLookUpTable, and jumps
  32. ; to the corresponding routine according to
  33. ; ASCIIKeyJumpTable. When the jumped-to routine returns, it
  34. ; will return directly to the code that called
  35. ; VectorOnASCIIKey. Ignores the key if the key code is not
  36. ; in the look-up table.
  37. ;
  38. ; Input: none
  39. ;
  40. ; Output: none
  41. ;
  42. ; Registers altered: AX, CX, DI, ES
  43. ;
  44. ; Direction flag cleared
  45. ;
  46. ; Table of 8-bit ASCII codes this routine handles.
  47. ;
  48. ASCIIKeyLookUpTable    label    word
  49.     db    02h    ;Ctrl-B to beep
  50.     db    18h    ;Ctrl-X to exit
  51. ;*** Additional ASCII codes go here ***
  52. ASCII_KEY_LOOK_UP_TABLE_LENGTH equ ($-ASCIIKeyLookUpTable)
  53. ;
  54. ; Table of addresses to jump to when corresponding key codes
  55. ; in ASCIIKeyLookUpTable are found.
  56. ;
  57. ASCIIKeyJumpTable    label    word
  58.     dw    Beep
  59.     dw    Exit
  60. ;*** Additional addresses go here ***
  61. ;
  62. VectorOnASCIIKey    proc    near
  63. WaitASCIIKeyLoop:
  64.     mov    ah,1    ;BIOS key status function
  65.     int    16h    ;invoke BIOS to see if
  66.             ; a key is pending
  67.     jz    WaitASCIIKeyLoop ;wait until key comes along
  68.     sub    ah,ah    ;BIOS get key function
  69.     int    16h    ;invoke BIOS to get the key
  70.     push    cs
  71.     pop    es
  72.     mov    di,offset ASCIIKeyLookUpTable
  73.             ;point ES:DI to the table of keys
  74.             ; we handle, which is in the same
  75.             ; segment as this code
  76.     mov    cx,ASCII_KEY_LOOK_UP_TABLE_LENGTH
  77.             ;# of bytes to scan
  78.     cld
  79.     repnz    scasb    ;look up the key
  80.     jnz    WaitASCIIKeyLoop ;it's not in the table, so
  81.                 ; ignore it
  82.     mov    di,ASCII_KEY_LOOK_UP_TABLE_LENGTH-1
  83.     sub    di,cx    ;calculate the # of the element we
  84.             ; found in ASCIIKeyLookUpTable.
  85.             ; The -1 is needed to compensate for
  86.             ; the 1-count overrun of SCAS
  87.     shl    di,1    ;multiply by 2 in order to perform
  88.             ; the look-up in word-sized
  89.             ; ASCIIKeyJumpTable
  90.     jmp    cs:[ASCIIKeyJumpTable+di]
  91.             ;jump to the routine for this key
  92. VectorOnASCIIKey    endp
  93. ;
  94. ; Code to handle Ctrl-X (ends the program).
  95. ;
  96. Exit    proc    near
  97.     mov    ah,4ch    ;DOS terminate program function
  98.     int    21h    ;exit program
  99. Exit    endp
  100. ;
  101. ; Code to handle Ctrl-B (beeps the speaker).
  102. ;
  103. Beep    proc    near
  104.     mov    ax,0e07h ;AH=0E is BIOS print character
  105.             ; function, AL=7 is bell (beep)
  106.             ; character
  107.     int    10h    ;tell BIOS to beep the speaker
  108.     ret
  109. Beep    endp
  110. ;
  111. code    ends
  112.     end    start
  113.